package com.gite.nouette.admin.implementation.integrator.builder;

import java.util.ArrayList;
import java.util.List;

import com.gite.nouette.admin.contract.integrator.assembler.command.IAssemblerCommandRemoveModule;
import com.gite.nouette.admin.contract.integrator.builder.IBuilderModule;
import com.gite.nouette.admin.contract.integrator.factory.IFactoryModel;
import com.gite.nouette.admin.contract.model.IManager;
import com.gite.nouette.admin.contract.model.command.ICommandCreateModule;
import com.gite.nouette.admin.contract.model.command.ICommandModule;
import com.gite.nouette.admin.implementation.integrator.assembler.command.AssemblerCommandRemoveModuleImpl;

public abstract class AbstractBuilderModule extends AbstractBuilder implements
		IBuilderModule {
	private IManager manager;
	private ICommandCreateModule commandCreateModule;

	public AbstractBuilderModule(IFactoryModel factoryModel, IManager manager,
			ICommandCreateModule commandCreateModule) {
		super(factoryModel);
		setManager(manager);
		setCommandCreateModule(commandCreateModule);
	}

	private IManager getManager() {
		return this.manager;
	}

	private void setManager(IManager manager) {
		this.manager = manager;
	}

	private ICommandCreateModule getCommandCreateModule() {
		return this.commandCreateModule;
	}

	private void setCommandCreateModule(ICommandCreateModule commandCreateModule) {
		this.commandCreateModule = commandCreateModule;
	}

	@SuppressWarnings("unchecked")
	protected ICommandModule[] getHeaderCommandModules() {
		List<ICommandModule> commandModules = new ArrayList<ICommandModule>();
		IAssemblerCommandRemoveModule commandRemoveModule = new AssemblerCommandRemoveModuleImpl(
				getFactoryModel(), getManager(), getCommandCreateModule());
		commandRemoveModule.setParentProduct(commandModules);
		commandRemoveModule.run();

		return commandModules.toArray(new ICommandModule[0]);
	}

	protected void initCommandModules() {
		List<ICommandModule> commandModules = getProduct().getHeaderCommands();
		for (ICommandModule commandModule : commandModules) {
			commandModule.setModule(getProduct());
		}
	}

	@Override
	public void buildCommandModules() {
		initCommandModules();
	}
}
